(function(win) {
	win.SITE_URL = location.href.replace(/(index\.html)?[#\w]*$/, '');
	var Z = win.Z;

	Z.loadScript(['lib/jquery', 'lib/jquery.cookie','lib/microtemplating','lib/Bundle', 'lib/Z', 'menus'], function() {
		function genMenus(menus){
			var menusArray = ['<ul>'];
			for(var i = 0, l = menus.length; i < l; i++){
				var item = menus[i];
				var menu = [];
				menu.push('<li>');
				menu.push('<a href="');
				if(typeof item.bundle == 'undefined'){
					menu.push('javascript:void(0);"');
				}else{
					menu.push('#', item.bundle, '"');
				}

				if(typeof item.className !== 'undefined'){
					menu.push(' class="menu-', item.className, '"');
				}

				menu.push('>');
				menu.push(item.label);
				menu.push('</a>');
				if(typeof item.children !== 'undefined'){
					menu.push(genMenus(item.children));
				}

				menu.push('</li>');
				menusArray.push(menu.join(''));
			}
			menusArray.push('</ul>');
			return menusArray.join('');
		}

		function success(data){
			$('ul#left li#menu').html(genMenus(win.menus));
			$('#menu a').click(function(){
				$(this).next('ul').toggle();
				var href = $(this).attr('href');
				if (/^javascript\:/.test(href)) {
					return false;
				}
				var params = Z.Bundle.getParams(href);
				Z.BundleList.load(params[0]);
				$('#menu li.active').removeClass('active');
				$(this).parent().parent().parent().addClass('active');
				$(this).parent().addClass('active');
			});
			var params = Z.Bundle.getParams();
			$('#menu a[href="#' + params[0] + '"]').parent().parent().show();
			$('#menu a[href="#' + params[0] + '"]').trigger('click');
			$('ul#left li#user-info img').attr('src', Z.url('/upload/' + data.avatar));
			$('ul#left li#user-info span:eq(0)').html(data.ip);
			$('ul#left li#user-info span:eq(1)').html(data.groupName + ' - ' +data.name);
		}

		$('ul#left li#user-info img').error(function(){
			var src = Z.url('/upload/avatar.png');
			if($(this).attr('src') != src) {
				$(this).attr('src', src);
			}
		});

		/*
		Z.tip = function(msg, seconds) {
			$('body').append('<div id="tip-1000000" class="global-tips">' + msg + '</div>');
			$('div#tip-1000000').animate({'top' : '5px'}, 1000, 'swing');
			var to = win.setTimeout(function(){
				$('div#tip-1000000').animate({'top' : '-100px'}, 1000, 'swing');
				win.clearTimeout(to);
			}, seconds || 3);
		}
		*/
		
		Z.ajax({
			'url' : Z.route('auth', 'login'),
			'success' : success,
			'error' : function(response) {
				var signForm = '<form action="#" method="post">\
					<div class="row"><label>您的大名</label><input type="text" name="name"/></div>\
					<div class="row button"><input type="submit" name="submit" value="提交"/></div>\
				</form>',
				form = new Z.Form({
					'title' : '请填写您的大名',
					'content' : signForm,
					'width' : 500,
					'action' : Z.route('auth','apply')
				});
				form.onsubmit = function(){win.location.reload();};
				form.open();
			}
		});

		win.uploadCallback = function(data) {
			Z.alert(data.msg);
		};


		var i = 1;
		$('body > a.switch').click(function(){
			if(i++ % 2) {
				$(this).animate({'left' : '0px'}, 200);
				$('body #right').animate({'left' : '10px'}, 200);
			} else {
				$(this).animate({'left' : '290px'}, 200);
				$('body #right').animate({'left' : '300px'}, 200);
			}
		});
		
	});
})(window);